﻿using MediatR;
using Sowrk03_28.Repository;
using Swork03_28.Api.Application.Commands.FarmedCommand;
using Swork03_28.Domain.TraceableInfos;

namespace Swork03_28.Api.Application.CommandHandlers.FarmedHandler
{
    public class FirmImgDelCommandHandler : IRequestHandler<FirmImgDelCommand, int>
    {
        private readonly IRepository<FirmInfo> _request;

        public FirmImgDelCommandHandler(IRepository<FirmInfo> request)
        {
            _request = request;
        }

        public async Task<int> Handle(FirmImgDelCommand request, CancellationToken cancellationToken)
        {
            var count = 0;
            var info = await _request.GetValues(x => x.BatchId == request.BatchId);
            var Imgs = info.FirstOrDefault().CampanyImage.Split(",").ToList();
            Imgs.RemoveAll(x => x == request.Img);
            info.FirstOrDefault().CampanyImage = string.Join(",", Imgs);
            count += await _request.UpdateAsync(info.FirstOrDefault());
            return count;
        }
    }
}
